package cn.xinfei.xdecision.common.service.analyse.job;

import cn.xinfei.xdecision.common.model.analyse.model.AnalyseDataModel;
import cn.xinfei.xdecision.common.model.analyse.model.AnalyseInfoModel;
import cn.xinfei.xdecision.common.model.analyse.model.OperatorEnum;
import cn.xinfei.xdecision.common.model.analyse.model.WindowEnum;
import cn.xinfei.xdecision.common.model.analyse.vo.AnalyseDataDetail;
import cn.xinfei.xdecision.common.service.analyse.AnalyseDataService;
import cn.xinfei.xdecision.common.service.analyse.AnalyseInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@Slf4j
public class SumOperatorJob extends AbstractOperator {

    @Autowired
    private AnalyseInfoService analyseInfoService;


    @Autowired
    private AnalyseDataService analyseDataService;

    public SumOperatorJob() {
        super(OperatorEnum.SUM);
    }

    public SumOperatorJob(OperatorEnum operatorEnum) {
        super(operatorEnum);
    }

    public void start() {

    }


    @Override
    public void calculate(AnalyseInfoModel info, AnalyseDataDetail data) {
        String uniqueKey = buildUniqueKey(info, data);
        Date bizDateTime = data.getBizDateTime();
        String windows = info.getWindows();
        Long cellTime = WindowEnum.getCellTime(windows);
        Long cellLine=bizDateTime.getTime()/cellTime;
        boolean saveOrUpdate=false;
        int retry=0;

        LambdaQueryWrapper<AnalyseDataModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnalyseDataModel::getUniqueKey, uniqueKey);
        wrapper.eq(AnalyseDataModel::getTimeLine, cellLine);
        while (saveOrUpdate==false&&retry<3){
            AnalyseDataModel one = analyseDataService.getOne(wrapper);
            if(one==null){
                AnalyseDataModel model=new AnalyseDataModel();
                model.setVersion(0L);
                model.setCellCnt(0L);
                model.setCellValue(data.getValue());
                model.setTimeLine(cellLine);
                model.setUniqueKey(uniqueKey);
                model.setUpdateTime(new Date());
                model.setCreateTime(new Date());
                saveOrUpdate = analyseDataService.save(model);
            }else{
                Long preVersion = one.getVersion();
                one.setVersion(preVersion +1);
                one.setCellCnt(one.getCellCnt() +1);
                one.setCellValue(one.getCellValue()+data.getValue());
                one.setUpdateTime(new Date());

                LambdaQueryWrapper<AnalyseDataModel> wrapperUpdate = new LambdaQueryWrapper<>();
                wrapper.eq(AnalyseDataModel::getUniqueKey, uniqueKey);
                wrapper.eq(AnalyseDataModel::getTimeLine, cellLine);
                wrapper.eq(AnalyseDataModel::getVersion, preVersion);
                saveOrUpdate = analyseDataService.update(one, wrapperUpdate);
            }
        }
        if(saveOrUpdate==false){
            //打点
            log.error("update data fail,info={},data={}",info,data);
        }else{
            log.info("update data success,info={},data={}",info,data);
        }
    }
}
